package com.jsgt.greekfire.common.util

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.ContextWrapper
import android.graphics.BitmapFactory
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import com.jsgt.greekfire.R

class NotificationUtil(context: Context?) : ContextWrapper(context) {
    companion object {
        const val downloadId = 19174436 //下载通知id
        const val sID = "channel_1"
        const val sName = "channel_name_1"
    }

    private var mManager: NotificationManager? = null
    fun sendNotification(title: String?, content: String?) {
        try {
            var notification: Notification? = null
            if (Build.VERSION.SDK_INT >= 26) {
                createNotificationChannel()
                notification = getNotification_26(title, content).build()
                notification.sound = null
                notification.vibrate = null
                getNotificationManager().notify(downloadId, notification)
            } else {
                notification = getNotification_25(title, content).build()
                notification.sound = null
                notification.vibrate = null
                getNotificationManager().notify(downloadId, notification)
            }
        } catch (e: Exception) {
        }
    }

    fun getNotification(title: String?, content: String?): Notification? {
        var notification: Notification? = null
        notification = if (Build.VERSION.SDK_INT >= 26) {
            createNotificationChannel()
            getNotification_26(title, content).build()
        } else {
            getNotification_25(title, content).build()
        }
        return notification
    }
    fun getNotificationManager():NotificationManager{
        if (mManager == null) {
            mManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        }
        return mManager!!
    }
    @RequiresApi(api = Build.VERSION_CODES.O)
    fun createNotificationChannel() {
        val channel = NotificationChannel(
            sID,
            sName,
            NotificationManager.IMPORTANCE_HIGH
        )
        channel.enableVibration(false)
        channel.enableLights(false)
        channel.setSound(null, null)
        getNotificationManager().createNotificationChannel(channel)
    }

    fun getNotification_25(
        title: String?,
        content: String?
    ): NotificationCompat.Builder {

        // 以下是展示大图的通知
        /*android.support.v4.app.NotificationCompat.BigPictureStyle style = new android.support.v4.app.NotificationCompat.BigPictureStyle();
        style.setBigContentTitle("BigContentTitle");
        style.setSummaryText("SummaryText");
        style.bigPicture(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));*/

        // 以下是展示多文本通知
        /*android.support.v4.app.NotificationCompat.BigTextStyle style1 = new android.support.v4.app.NotificationCompat.BigTextStyle();
        style1.setBigContentTitle(title);
        style1.bigText(content);*/
        return NotificationCompat.Builder(applicationContext)
            .setContentTitle(title)
            .setContentText(content)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(
                BitmapFactory.decodeResource(
                    resources,
                    R.mipmap.ic_launcher
                )
            ) //.setStyle(style)
            .setAutoCancel(true)
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    fun getNotification_26(
        title: String?,
        content: String?
    ): Notification.Builder {
        return Notification.Builder(
            applicationContext,
            sID
        )
            .setContentTitle(title)
            .setContentText(content)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(
                BitmapFactory.decodeResource(
                    resources,
                    R.mipmap.ic_launcher
                )
            ) //.setStyle(new Notification.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)))
            .setNumber(1)
            .setAutoCancel(true)
    }


}